<%@ page language="java" contentType="text/html;charset=UTF-8" %><%@ include file="../../inc/global.jsp" 
%>
<!doctype html>
<html>
<head>
<title>${adminGlobalConfig.name}</title>
<style type="text/css">
.cbx{margin:7px 0px 0px 0px}
.cbx span{
	margin:0px 3px 7px 3px;
}
.list table tr {cursor:pointer;}
#ajaxForm {margin:0px auto; width:98%;}
#ajaxForm th {width:80px;}
#ajaxForm th, #ajaxForm table tr td {height:30px;}
#del {margin-left:80px;}
</style>
<%@include file="../head.jsp" %>
<script type="text/javascript">
function StoreHouse() {
	var _this=this;
	this.init = function() {
		$('#name').poshytip({
			className: 'tip-yellow',
			showOn: 'focus',
			alignTo: 'target',
			alignX: 'center',
			alignY: 'bottom',
			offsetX: 0,
			offsetY: 5
		});
		$('#button').click(function(){
			_this.save();
		});
		bindClickEvents();
	};
	function bindClickEvents() {
		$('table tr').unbind();
		$('table tr').click(function(){
			var id = $(this).attr('id');
			_this.showForm(id);
		});
		$('table tr').mouseout(function(){
			$(this).css('background','#fff');
		}).mouseover(function(){
			$(this).css('background','#ffffbb');
		});
	};
	function remind(msg, time) {
		if(time==undefined) {
			time=3000;
		}
		$('#button').poshytip({
			content: function(){return msg},
			timeOnScreen: time,
			className: 'tip-yellow',
			showOn: 'none',
			alignTo: 'target',
			alignX: 'right',
			alignY: 'center',
			offsetX: 0,
			offsetY: 5
		}).poshytip('show');
	};
	function doSave(id, name, success, exception, error) {
		var url='/a/ajax/store/house/save';
		$.ajax({
			url:url,
			type:'POST',
			data:{id:id,name:name},
			success:function(data){
				var json=$.parseJSON(data);
				if(json.succeed) {
					success(json.data.id,json.data.name);
				}
				else {
					exception();
				}
			},
			error:function(){
				error();
			}
		});
	};
	this.save = function() {
		var name=$('#name').val();
		var id=$('#id').val();
		doSave(id, name, function(id, name){
			var tr='<tr id="'+id+'">';
			tr+='<td>'+name+'</td>';
			tr+='<td>0</td>';
			tr+='<td></td>';
			tr+='<td></td>';
			tr+='</tr>';
			$(tr).prependTo('.list table > tbody');
			//$('.list table tbody').append(tr);
			bindClickEvents();
			remind('保存成功！');
		}, function(){
			$('#name').poshytip('show');
		}, function(){
			alert('系统错误请联系管理员！');
		});
	};
	var bg, dt;
	function hide() {
		aju.removeAll([dt,bg,$('.tip-yellow')]);
		$('#ajaxClose').remove();
	};
	this.showForm=function(id) {
		var url="/a/ajax/store/house/form/"+id;
		$.ajax({
			type:'GET'
			, url:url
			, success:function(data){
				bg = aju.ajaxBg(function() {
					aju.setOffsetDown(200);
					aju.setOffsetRight(10);
					dt = aju.ajaxData().css({'width':'600','height':'180'});
					dt.html(data);
					aju.positionCenter(dt);
					// 显示关闭按钮并绑定关闭事件
					aju.ajaxClose(dt).click(function(){
						hide();
					});
				});
				$('#ajaxForm input[name=name]').focus();
				//
				$(document).bind('keydown', function(event){
					if (event.keyCode == KEY.ESC) {
						hide();
					}
				});
				//
				$("#edit").click(function(){
					var name=$('#ajaxForm input:text[name=name]').val();
					var id=$('#ajaxForm input:hidden[name=id]').val();
					$("#edit").attr('disabled', 'disabled');
					var url="/a/ajax/store/house/save";
					$.ajax({
						url:url
						,type:'POST'
						,data:{id:id,name:name}
						,success:function(data){
							var json=$.parseJSON(data);
							var msg;
							if(json.succeed) {
								$('#'+json.data.id+" td:first").html(json.data.name);
								msg='修改成功';
							}
							else {
								msg=json.msg;
							}
							$('#edit').poshytip({
								content: msg,
								showOn: 'none',
								timeOnScreen: '5000',
								className: 'tip-yellow',
								alignTo: 'target',
								alignX: 'center',
								alignY: 'bottom',
								offsetX: 0,
								offsetY: 5
							}).poshytip('show');
							$("#edit").removeAttr('disabled');
						}
						,error:function(){
							alert('系统错误请联系管理员！');
						}
					});
				});
				$("#del").click(function(){
					var name=$('#ajaxForm input:text[name=name]').val();
					var id=$('#ajaxForm input:hidden[name=id]').val();
					$(this).html('<span class="red"><fmt:message key="message.del.loading"/></span>');
					var url="/a/ajax/store/house/del/"+id;
					$.ajax({
						url:url
						, type:'POST'
						, success:function(data){
							var json = $.parseJSON(data);
							if(json.succeed) {
								$('#del').html('<span class="red"><fmt:message key="remind.succeed"/></span>');
								$('#'+json.data.id).remove();
							}
							else {
								$('#del').poshytip({
									content: json.msg,
									showOn: 'none',
									timeOnScreen: '5000',
									className: 'tip-yellow',
									alignTo: 'target',
									alignX: 'center',
									alignY: 'bottom',
									offsetX: 0,
									offsetY: 5
								}).poshytip('show');
								$("#del").html('<fmt:message key="message.del"/>');
							}
						}, error:function(){
							$("#del").html('<fmt:message key="message.del"/>');
							alert('系统错误请联系管理员！');
						}
						
					});
				});
			}
			, error:function(){
				alert('系统错误请联系管理员！');
			}
		});
	};
}
var storeHouse = new StoreHouse();
$(function(){
	defaultKeys('name');
	storeHouse.init();
});
</script>
</head>
<body>
<div class="wrapper">
<%@include file="../header.jsp" %>
<div class="body">
<div class="list">
<div class="listnav">
<input type="text" id="name" name="name" value="输入库房名称或库房代号" title="输入库房名称或库房代号"/>
<input type="button" id="button" name="button" value='<fmt:message key="message.storehouse.button.add" />' />
</div>
<table>
<thead>
<tr>
<th><fmt:message key="message.storehouse.name" /></th>
<th><fmt:message key="message.storehouse.total" /></th>
<th><fmt:message key="message.storehouse.latestInTime" /></th>
<th><fmt:message key="message.storehouse.latestOutTime" /></th>
</tr>
</thead>
<tbody>
<c:forEach var="item" items="${houses}">
<tr id="${item.id}">
<td>${item.name}</td>
<td>${item.total}</td>
<td>${item.latestInTime}</td>
<td>${item.latestOutTime}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div class="sidebarMenu">
</div>
<%@ include file="sidebar.jsp" %>
</div>
</div>
</body>
</html>